<?php
/**
 * SimpleDatexV.php
 * 
 * This file contains the SimpleDatexV class, an object representation of joined table rows.
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage dataaccess.wpdatex
 */

/**
 * An ORM implementation for multiple data rows over multiple tables
 * 
 * The SimpleDatexV class is an object representation of several rows of tables connected by the join statement. The instance 
 * of this class will represent a number of rows exactly like SimpleDatexM does. But instead of targeting a table or view, this class 
 * is able to target several tables/views. Its beahviour ist mostly like being a view but more flexible. This is an abstract 
 * class, so it is necessary to inherit this class and implement the abstract funtions which mainly describe the join operation. 
 * Also, as this table inherits SimpleDatexM, there are a couple of abstract methods more to be implemented.
 * 
 * Extending this class:
 * <code>
 * class MyData extends SimpleDatexV
 * {
 * 		//Example joining table1 - table2 - table3
 * 
 * 		//Implement the abstract getTables method
 * 		public function getTables() {
 * 			return array('table1','table2','table3');
 * 		}
 * 
 * 		//Implement the abstract getJoinAddCondition method
 * 		public function getJoinAddCondition($tablename)
 * 		{
 * 			if ($tablename === 'table2') {
 * 				return 'table2.status = \'new\'';
 * 			}
 * 
 * 			return '';
 * 		}
 * 
 * 		//Implement the abstract getJoinKey method
 * 		public function getJoinKey($tablename)
 * 		{
 * 			switch ($tablename) {
 * 				case 'table1' : '';
 * 				case 'table2 : 'id_table1';
 *				case 'table3 : 'id_table2';
 * 			}
 * 		}
 * 		
 * 		//Implement the abstract getJoinOperator method
 * 		public function getJoinOperator($tablename)
 * 		{
 * 			return '=';
 * 		}
 * 
 * 		//Implement the abstract getJoinTarget method
 * 		public function getJoinTarget($tablename)
 * 		{
 * 			switch ($tablename) {
 * 				case 'table1' : '';
 * 				case 'table2 : 'table1';
 *				case 'table3 : 'table2';
 * 			}
 * 		}
 * 
 * 		//Implement the abstract getJoinTargetKey method
 * 		public function getJoinTargetKey($tablename)
 * 		{
 * 			return 'id';
 * 		}
 * 
 * 		//Implement the abstract getJoinType method
 * 		public function getJoinType($tablename)
 * 		{
 * 			return 'INNER JOIN';
 * 		}
 * 
 * 		//Implement the abstract getFields method (from SimpleDatexM)
 * 		public function getFields()
 * 		{
 * 			return array(
 * 				'table1.id',
 * 				'table1.name',
 * 				'table1.status',
 * 				'table2.id',
 * 				'table2.name',
 * 				'table2.status',
 * 				'table2.id_table1',
 * 				'table3.id',
 * 				'table3.name',
 * 				'table3.status',
 *				'table3.id_table2'
 * 			);
 * 		}
 * 		
 * 		//Implement the abstract getFieldType method (from SimpleDatexM)
 * 		public function getFieldType($fieldname)
 * 		{
 * 			if ($fieldname === 'table1.id' ||
 * 				$fieldname === 'table2.id' ||
 * 				$fieldname === 'table3.id' ||
 * 				$fieldname === 'table2.id_table1' ||
 * 				$fieldname === 'table1.id_table2') {
 * 				return 'number';
 * 			}
 * 
 * 			return 'string';
 * 		}
 * }
 * </code>
 *   
 * @see SimpleDatexM 
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage dataaccess.wpdatex
 */
abstract class SimpleDatexV extends SimpleDatexM
{
	/**
	 * Retrieves the additional condition to be used in the ON clause (besides the keys)
	 * @param string $alias The alias of the table to get the additional condition for. The condition 
	 * MUST NOT contain the operator (AND/OR). It will be placed automatically and is always AND.  
	 * @return string The condition as string, empty string if no condition needs to be append
	 */
	public abstract function getJoinAddCondition($alias);

  /**
   * Retrieves the field name used to join to another table 
   * @param string $alias The alias of the table to retrieve the join key column name for
   * @return string The field name used for join
   */
	public abstract function getJoinKey($alias);
	
	/**
	 * Retrieves the operator used to join the keys (=, !=, <, >)
	 * @param string $alias The alias of the table to retrieve the join operator for
	 * @return string The join operator
	 */
	public abstract function getJoinOperator($alias);
	
	/**
	 * Retrieves the table to target when joining
	 * @param string $alias The alias of the table the target table should be retrieved of
	 * @return string The target table name
	 */
	public abstract function getJoinTarget($alias);
	
	/**
	 * Retrieves the fieldname in the target table which will be used for the ON clause when joining
	 * @param string $alias The alias to retrieve the target key field of
	 * @return string The fieldname (as simple name, without table identifier)
	 */
	public abstract function getJoinTargetKey($alias);
	
	/**
	 * Retrieves the join type to use
	 * @param $alias The table alias to retrieve the join type of 
	 * @return string The join type to use ("INNER JOIN", "LEFT JOIN", ...) 
	 */
	public abstract function getJoinType($alias);
	
  /**
   * @see SimpleDatexS::getTable()
   */
  public function getTable()
  {
      $ret = '';
  	$tabs = $this->getTables(); //--> Aliases
      
      for ($i=0; $i<count($tabs); $i++) {
      	$salias = $tabs[$i];
      	$tab = $this->getTableName($salias);
      	
      	//-- Join params --
      	if ($i > 0) {
      		$jt = $this->getJoinType($salias); 
      		$ret .= ' '.$jt.' ';
      	}
      	
      	$ret .= $salias !== '' ? $tab.' '.$salias : $tab;
      	
      	if ($i > 0) {
      		$k = $this->getJoinKey($salias);
      		$o = $this->getJoinOperator($salias);
      		$t = $this->getJoinTarget($salias);
      		$tk = $this->getJoinTargetKey($salias);
      		$ad = $this->getJoinAddCondition($salias);
      		
      		$s = $salias !== '' ? $salias : $tab;
      		$ret .= ' ON '.$s.'.'.$k.$o.$t.'.'.$tk;
      		$ret .= $ad !== '' ? ' AND '.$ad : ''; //Append additional condition 
      	}
      	$ret .= ' ';  	
      }
  	
  	return $ret;
  }
    
 
  /**
   * @see SimpleDatexV::getTableName()
   */
	public abstract function getTableName($alias);
    

  /**
   * Retrieves an array of aliases of tables to be joined.
   * @return array An array of table aliases
   */
  public abstract function getTables();
    
  /**
   * @see SimpleDatexM::isView()
   */
  public function isView() {
      return true;
  }
}

/* End of file SimpleDatexV.php */
/* Location: ./wildpack/dataaccess/wpdatex/SimpleDatexV.php */  
